﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AxWMPLib;
using WMPLib;
using AccompanyCabinet.Interface;
using AccompanyCabinet.Provider;
namespace AccompanyCabinet.Common
{
    public enum WMPActionType
    {
        Play =0,
        Stop =1,
        Pause =2
    }


    /// <summary>
    /// windows media player 
    /// </summary>
    public class WMPManager: IWMPMangerProvider
    {
        /// <summary>
        /// windows media play 播放器组建实例
        /// </summary>
        public AxWindowsMediaPlayer WMP { get; private set; }
        /// <summary>
        /// 播放列表
        /// </summary>
        public IWMPPlaylist PlayList { get; private set; }

        /// <summary>
        /// 消息提供器
        /// </summary>
        public IMessageProvider MessageProvider { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="wmp"></param>
        /// <param name="files"></param>
        public WMPManager(AxWindowsMediaPlayer wmp,string [] files=null,IMessageProvider messageProvider =null)
        {
            this.WMP = wmp;
            if (files != null && files.Length>0)
            {
                LoadPlayList(files);
            }
            if(messageProvider == null)
            {
                this.MessageProvider = new CustomeMessageProvider(null);
            }
        }

        /// <summary>
        /// 加载播放列表
        /// </summary>
        /// <param name="fileNames"></param>
        public void LoadPlayList(string[] fileNames)
        {
            if(fileNames == null || fileNames.Length <=0)
            {
                return;
            }

            IWMPMedia media;
            this.PlayList = WMP.playlistCollection.newPlaylist("MyPlayList"); //新建列表
            foreach (var item in fileNames)
            {
                media = WMP.newMedia(item); //参数为歌曲路径
                this.PlayList.appendItem(media);
            }
            this.WMP.currentPlaylist = this.PlayList;
        }
        /// <summary>
        /// 重新加载播放列表
        /// </summary>
        /// <param name="fileNames"></param>
        /// <param name="beforeReloadCallback"></param>
        /// <param name="afterReloadCallback"></param>
        public void ReloadPlayList(string[] fileNames,Action beforeReloadCallback=null,Action afterReloadCallback=null)
        {
            beforeReloadCallback?.Invoke();
            this.ClearPlayList();
            this.LoadPlayList(fileNames);
            afterReloadCallback?.Invoke();
        }
        /// <summary>
        /// 清空播放列表
        /// </summary>
        public void ClearPlayList()
        {
            this.WMP.currentPlaylist.clear();
        }
        /// <summary>
        /// 管理播放器
        /// </summary>
        /// <param name="actionType"></param>
        public void ActionManager(WMPActionType actionType)
        {
            switch (actionType)
            {
                case WMPActionType.Play:WMP.Ctlcontrols.play();
                    break;
                case WMPActionType.Stop:WMP.Ctlcontrols.stop();
                    break;
                case WMPActionType.Pause:WMP.Ctlcontrols.pause();
                    break;
                default:
                    break;
            }
        }
    }
}
